import { QueryClient, useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { AxiosError } from "axios";
import { ModelFileService } from "@/shared/api/api.model-file";
import { ImageFileService } from "@/shared/api/api.image-file";

export function useUploadImage() {
    const { register, handleSubmit } = useForm<{
        file: File[]
    }>();
    const queryClient = new QueryClient

    const uploadImageMutation = useMutation(
        {
            mutationFn: ImageFileService.uploadFile,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const isError = uploadImageMutation.isError;

    return {
        register,
        isError,
        response: uploadImageMutation.data,
        isPending: uploadImageMutation.isPending,
        isSuccess: uploadImageMutation.isSuccess,
        handleSubmit: handleSubmit((data) => uploadImageMutation.mutate({ ...data })),
    };
}